/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
#include <QMessageBox>
#include <QDebug>
#include "menu.h"
#include "menumodel.h"
#include "menuwidgetinterface.h"
#include "addwidetinterface.h"

Menu::Menu(QWidget* parent, Qt::WFlags fl)
		: KontAMabilWindow(parent, fl), Ui::menuForm()
{
	setupUi(this);
	QPixmap pixmap(":/images/splash.png");
	QSplashScreen splash(pixmap);
	splash.show();
	if (!kontAMabil()->loadQuerys(&splash))
	{
		QMessageBox::critical(this, tr("Login"), tr("Prea multe erori.\nAplicatia nu poate continua."),QMessageBox::Ok);
		close();
		return;
	}
	splash.finish(this);
	MenuModel * model= new MenuModel(this);
	QStringList menus;
	foreach(MenuWidgetInterface * menu, kontAMabil()->menuPlugins())
		menus<<menu->showMenus();
	model->setMenus(menus);
	foreach(MenuWidgetInterface * menu, kontAMabil()->menuPlugins())
		model->addMenu(menu);
	menu->setModel(model);

	connect(menu, SIGNAL(finalSelectedIndex(QModelIndex)),SLOT(startPlugin(QModelIndex)));
	connect(menu, SIGNAL(activated( const QModelIndex& )),SLOT(showDoc(const QModelIndex&)));
	connect(menu, SIGNAL(entered( const QModelIndex& )),SLOT(showDoc(const QModelIndex&)));
	connect(menu, SIGNAL(pressed( const QModelIndex& )),SLOT(showDoc(const QModelIndex&)));
	connect(menu, SIGNAL(clicked( const QModelIndex& )),SLOT(showDoc(const QModelIndex&)));
	connect(menu, SIGNAL(clicked( const QModelIndex& )),SLOT(startPlugin(QModelIndex)));
}

void Menu::showDoc(const QModelIndex & index)
{
	if (reinterpret_cast<MenuModel::MenuStruct*>(index.internalPointer())->plugin)
		doc->setHtml(reinterpret_cast<MenuModel::MenuStruct*>(index.internalPointer())->plugin->description(reinterpret_cast<MenuModel::MenuStruct*>(index.internalPointer())->fullMenu));
	else
	{
		QString html="<h1>Submeniuri</h1>";
		foreach(MenuModel::MenuStruct* submenus, reinterpret_cast<MenuModel::MenuStruct*>(index.internalPointer())->children)
			html+=QString("<h2>%1</h2").arg(submenus->text);
		doc->setHtml(html);
	}
}

void Menu::startPlugin(const QModelIndex & index)
{
	if(reinterpret_cast<MenuModel::MenuStruct*>(index.internalPointer())->plugin)
	{
		KontAMabilWindow * m_showedObject=reinterpret_cast<MenuModel::MenuStruct*>(index.internalPointer())->plugin->createWidget();
		if (m_showedObject)
		{
			connect(m_showedObject,SIGNAL(aboutToClose()),SLOT(show()));
			connect(m_showedObject,SIGNAL(aboutToClose()),m_showedObject, SLOT(deleteLater()));
			hide();
			m_showedObject->show();
		}
		else
			reinterpret_cast<MenuModel::MenuStruct*>(index.internalPointer())->plugin->exec(reinterpret_cast<MenuModel::MenuStruct*>(index.internalPointer())->fullMenu);
	}
}

Menu::~Menu()
{
}

